package com.wokeMy.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.example.woke.PeaceActivity;
import com.http.HttpMethods;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.woke.adapter.ExChangeViewProvider;
import com.woke.adapter.OnItemClickListener;
import com.woke.data.UserPromoteCode;
import com.woke.method.NetworkUtils;
import com.zhongcai.online.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

import static java.security.AccessController.getContext;

public class CreditCardActivity extends AppCompatActivity implements XRecyclerView.LoadingListener {
    private static final String TAG = CreditCardActivity.class.getSimpleName();
    private XRecyclerView mRecyclerView;
    private List<UserPromoteCode> mExchangeList = new ArrayList<>();
    public static final int TYPE_LOAN = 1; // 贷款
    public static final int TYPE_CREDIT = 2;// 信用卡
    private MultiTypeAdapter mMulitypeAdapter;
    private Items mItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_card);
        findViewById(R.id.creditCard_image_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.huanKuan_credit_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreditCardActivity.this,CreditRepayActivity.class);
                startActivity(intent);
//                Toast.makeText(CreditCardActivity.this,"信用卡还款暂未开通",Toast.LENGTH_SHORT).show();
            }
        });
        mRecyclerView = (XRecyclerView) findViewById(R.id.creditCard_recyclerView);
        View headerView = LayoutInflater.from(this).inflate(R.layout.exchange_header,null);
        headerView.findViewById(R.id.zj_banner);
        if(getResources().getString(R.string.oem_app_name).equals("中财在线")){
            headerView.setBackgroundResource(R.mipmap.creditheaderimg_zc);

        }else {
            headerView.setBackgroundResource(R.mipmap.creditheaderimg);
        }
        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreditCardActivity.this,CreditRepayActivity.class);
                startActivity(intent);
            }
        });
        mRecyclerView.addHeaderView(headerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(this);
        initMulitypeAdapter();
        getpRromoteFromHttp();

    }
    private void initMulitypeAdapter() {

        mItems = new Items();
        mMulitypeAdapter = new MultiTypeAdapter();
        mMulitypeAdapter.register(UserPromoteCode.class, new ExChangeViewProvider(mItemClickListener));
        mRecyclerView.setAdapter(mMulitypeAdapter);
        mRecyclerView.setLoadingMoreEnabled(false);
        mMulitypeAdapter.setItems(mItems);
        mMulitypeAdapter.notifyDataSetChanged();
        //		if (mItems.size () > 0) {
        //			assertAllRegistered (mMulitypeAdapter, mItems);//执行这句话时,必须保证 Items.size()>0
        //		}

    }

    private OnItemClickListener mItemClickListener = new OnItemClickListener() {
        @Override
        public void OnItemClick(View view, int position) {

        }

        @Override
        public void OnItemClick(View view, Object object, int position) {

            if (object instanceof UserPromoteCode) {
                UserPromoteCode code = (UserPromoteCode) object;
                Intent intent = new Intent(CreditCardActivity.this, PeaceActivity.class);
                intent.putExtra("url", code.getLink());
                intent.putExtra("title", code.getName());
                startActivity(intent);
            }
        }
    };


    public void getpRromoteFromHttp() {
        HttpMethods.getInstance().exchangeOrgList(mOrgListObserver, 2);
    }
    private Observer<List<UserPromoteCode>> mOrgListObserver = new Observer<List<UserPromoteCode>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(List<UserPromoteCode> value) {
            if (value != null && value.size() > 0) {
                mItems.clear();
                mItems.addAll(value);
                mItems.addAll(mExchangeList);
                if (mMulitypeAdapter != null) {
                    mMulitypeAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onError(Throwable e) {
            if (mRecyclerView != null) {
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }

        @Override
        public void onComplete() {
            if (mRecyclerView != null) {
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }
    };

    @Override
    public void onRefresh() {
        if (getContext() != null) {
            if (NetworkUtils.isConnected(this)) {
                getpRromoteFromHttp();
            } else {
                mRecyclerView.refreshComplete();
            }
        }
    }

    @Override
    public void onLoadMore() {
        mRecyclerView.loadMoreComplete();
    }
}
